import { ref } from "vue"
export default function (props, emit) {
  // _.clearlocalStorage()
  const keywords = ref('')
  const listObj = ref({
    isLoad: true,
    list: _.getLocalStorage('list') || [],
    isEnd: false,
  })
  const isShowAdd = ref(false)
  // 添加网址
  async function addUrl(url){
    const data = await _.invoke('getUrl', {url})
    // console.log(data)
    if(listObj.value.list.some(v => v.name == data.name)) {
      return window.electronAPI.invoke('alertEvent', '此网站已经被添加');
    }
    listObj.value.list = [data, ...listObj.value.list]
    _.setLocalStorage('list', listObj.value.list)
  }
  function delUrl(id){
    listObj.value.list = listObj.value.list.filter(v => v.id != id)
    _.setLocalStorage('list', listObj.value.list)
  }
  function openUrl(item){
    // window.open(item.url, '_blank', 'width=1920,height=1080');
    _.invoke('openUrl', {url:item.url})
  }
  function searchVal(){
    listObj.value.list = listObj.value.list.map(v => {
      v.isShow = v.name.includes(keywords.value)
      return v
    })
  }
  // 注册监听事件
  window.electronAPI && window.electronAPI.on('addUrl', (data) => {
    isShowAdd.value = true
  })
  return {
    keywords, listObj, isShowAdd,
    emit, addUrl, delUrl, openUrl, searchVal
  }
}